package com.sunnypower.rushfitapp.BodyDataPart;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.sunnypower.rushfitapp.HistoryBodyDataActivity;
import com.sunnypower.rushfitapp.R;
import com.sunnypower.rushfitapp.db.BodyData;
import com.sunnypower.rushfitapp.util.ParseStringToOther;

import org.litepal.LitePal;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BodyDataActivity extends AppCompatActivity {
    private static final String NO_DATA = "-";// 无数据时显示-
    private static final String TAG = "BodyDataActivity";

    private TextView update_date;// 更新时间
    private TextView height;// 身高
    private TextView weight;// 体重
    private TextView fatRate;// 体脂率
    private TextView waist;// 腰围
    private TextView hipline;// 臀围
    private TextView chest;// 胸围
    private TextView thigh;// 腿围
    private Button bt_add_data;// 更新数据
    private Button bt_history_body;
    private Button btn_back;
    private List<BodyData> dataList = new ArrayList<>();
    private Integer sex;
    private Integer uid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_body_data);
        // 初始化控件
        update_date = findViewById(R.id.update_date);
        height = findViewById(R.id.height);
        weight = findViewById(R.id.weight);
        fatRate = findViewById(R.id.fatRate);
        waist = findViewById(R.id.waist);
        hipline = findViewById(R.id.hipline);
        chest = findViewById(R.id.chest);
        thigh = findViewById(R.id.thigh);
        bt_add_data = findViewById(R.id.bt_add_data);
        bt_history_body = findViewById(R.id.bt_history_body);
        btn_back = findViewById(R.id.btn_back);
        sex = getIntent().getIntExtra("sex", 1);
        uid = ParseStringToOther.strToint(getIntent().getStringExtra("uid"));
        SQLiteDatabase db = LitePal.getDatabase();

        getData();
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        bt_add_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BodyDataActivity.this, AddBodyData.class);
                intent.putExtra("sex", sex);
                startActivityForResult(intent, 1);
            }
        });
        bt_history_body.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BodyDataActivity.this, HistoryBodyDataActivity.class);
                intent.putExtra("all_data", (Serializable)dataList);
                startActivity(intent);
            }
        });
    }

    /**
     * 处理通过Intent获得的数据
     * 1：获取体测记录
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:// 获取体测记录
                if (resultCode == RESULT_OK) {
                    // 获取当前时间并设置
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    BodyData bodyData = (BodyData)data.getSerializableExtra("new_data");
                    bodyData.setTime(sdf.format(date));
                    bodyData.setUid(uid);
                    Log.d(TAG, "onActivityResult: " + bodyData);
                    bodyData.save();
                    Toast.makeText(getApplicationContext(), "保存数据成功！", Toast.LENGTH_SHORT);
                    getData();
                }
        }
    }

    /**
     * 刷新数据
     */
    private void getData() {
        dataList = LitePal.where("uid = ?", uid.toString()).find(BodyData.class);
        Log.d(TAG, "getData: "+dataList);
        if (dataList.size() == 0) {// 无数据初始化
            update_date.setText(NO_DATA);
            height.setText(" " + NO_DATA);
            weight.setText("  " + NO_DATA);
            fatRate.setText("  " + NO_DATA);
            waist.setText("  " + NO_DATA);
            hipline.setText("  " + NO_DATA);
            chest.setText("  " + NO_DATA);
            thigh.setText("  " + NO_DATA);
        } else {
            BodyData bodyData_latest = dataList.get(dataList.size() - 1);// 获取最新数据
            update_date.setText(bodyData_latest.getTime());
            height.setText(" " + bodyData_latest.getHeight());
            weight.setText(" " + bodyData_latest.getWeight().toString());
            fatRate.setText(" " + bodyData_latest.getFatRate().toString().substring(2, 4) + "%");
            waist.setText(" " + bodyData_latest.getWaist().toString());
            hipline.setText(" " + bodyData_latest.getHipline().toString());
            chest.setText(" " + bodyData_latest.getChest().toString());
            thigh.setText(" " + bodyData_latest.getThigh().toString());
        }
    }
}
